package com.aiyige.page.my.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.BuildConfig;
import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventAgreement;
import com.aiyige.logic.user.UserDataManager;
import com.aiyige.model.TodoTasksModel;
import com.aiyige.model.User;
import com.aiyige.model.request.GetUserTodoTasks;
import com.aiyige.page.my.wallet.utils.WalletServiceModel;
import com.aiyige.page.my.wallet.utils.WalletUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.DialUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.TitleLayout;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的钱包
 */
@Route(path = ARouterConfig.WalletHomePage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WalletHomePage extends BaseActivity {

    @BindView(R.id.tv_details)
    TextView tvDetails;
    @BindView(R.id.setting_title_layout)
    TitleLayout settingTitleLayout;
    @BindView(R.id.iv_instructions)
    ImageView ivInstructions;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.btn_submit)
    View btnSubmit;
    @BindView(R.id.ll_call_wallet_service)
    LinearLayout llCallWalletService;
    public static String WALLET_CODE = null;

    WalletServiceModel walletServiceModel = new WalletServiceModel();
    WalletUtil walletUtil = new WalletUtil();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wallet_home);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.my_wallet), false);
        walletUtil.getServiceNumber(walletServiceModel);
        setAutoRegisterEventBus(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshViews();
    }

    private void refreshViews() {
        User user = AccountUtil.getCurrentUser();
        if (user != null && user.getStatisticsBackup() != null) {
            double balance = user.getStatisticsBackup().getPurseBalance();
            tvBalance.setText(StringUtils.priceFormat(balance));
        }
    }


    @Subscribe
    public void onEventAgreement(EventAgreement event) {
        if (event != null) {

        }
    }

    int switchi = 3;

    @OnClick({R.id.tv_details, R.id.iv_instructions, R.id.btn_submit, R.id.ll_call_wallet_service})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_details:
                ARouter.getInstance()
                        .build(ARouterConfig.WalletDetailsListPage)
                        .navigation();
                break;
            case R.id.iv_instructions:
                ARouter.getInstance()
                        .build(ARouterConfig.WebViewPage)
                        .withString("url", BuildConfig.walletDetailUrl)
                        .withBoolean("disableNav", false)
                        .withBoolean("noTitleBar", false)
                        .navigation();
                break;
            case R.id.btn_submit:
//                ARouter.getInstance()
//                        .build(ARouterConfig.WalletWithdrawalPage)
//                        .navigation();
                UserDataManager.getInstance().requestUserTodoTasks(GetUserTodoTasks.TYPE_WITHDRAW, new UserDataManager.UserTodoCallback() {
                    @Override
                    public void onTodoTask(String task) {
                        if (isNull(task)) {
                            return;
                        }
                        switch (task) {
                            // 用户认证
                            case TodoTasksModel.TASK_USER_CERTIFICATION:{

                            }
                            // 用户商品合作协议
                            case TodoTasksModel.TASK_COMMODITY_COOPERATION:{

                            }
                            default:{
                                onNothingTask();
                            }

                        }
                    }

                    @Override
                    public void onNothingTask() {
                        if (AccountUtil.hasWalletPassword()) {
                            ARouter.getInstance()
                                    .build(ARouterConfig.WalletWithdrawalPage)
                                    .navigation();
                        } else {
                            ARouter.getInstance()
                                    .build(ARouterConfig.WalletVerifyCodePage)
                                    .navigation();
                        }
                    }
                });


                break;
            case R.id.ll_call_wallet_service:
                DialUtil.dial(walletServiceModel.getServicePhone());
                break;
        }
    }
}
